#include "dumpthread.h"
#include "QThread"
#include "QDebug"
#include "mainwindow.h"

DumpThread::DumpThread(QObject *parent) : QThread(parent)
{

}

int DumpThread::connectDev()
{
//    return cnn_dev (&handle,&di,0,0);
    //    emit sigConnecetDev();
    return 0;
}

void DumpThread::disconnectDev()
{

}

void DumpThread::run()
{
    timer = new QTimer();
    connect(timer, SIGNAL(timeout()), this, SLOT(timerOutSlot()), Qt::DirectConnection);
    timer->start(1000);
    exec();
}

void DumpThread::connectDevSlot()
{
    //    discnn_dev (handle,0);
}

void DumpThread::timerOutSlot()
{
    QString devStr;
    int devNum = 0;
    MainWindow *pParent = (MainWindow *)parent();
    devNode node;

//    pParent->devList.printNodeList();
    cnn_dev(&handle,&di,0,0);
    if(handle){
        qDebug()<<"found"<<devStr.sprintf("Checkme_%s", (NULL == di.sn) ? "N/A" : (const char *) di.sn);

        if(pParent->devList.findnode(devStr, devNum) == NULL){
            node.devInfo = di;
            node.devName = devStr;
            node.handle = handle;
            node.state = NST_CNNED;
            pParent->devList.addNode(node);
        }

    }
    for(int i = 0; i < MAX_CONN_DEV; i++){
        if(pParent->devList.getNode(i, node) < 0)
            break;
        if(ping_dev(node.handle,0)){
            qDebug()<<"pingok";
        }else{
            qDebug()<<"pingelse";
        }
    }


//    pParent->devList.getNode(0, node);
}

